package mil.af.amc.adpetracker.dao.hibernate;

import mil.af.amc.adpetracker.dao.HandReceiptDao;
import mil.af.amc.adpetracker.model.AdpeAccount;
import mil.af.amc.adpetracker.model.HandReceipt;
import mil.af.amc.adpetracker.model.User;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * Hibernate implementation of the HandReceiptDao interface
 *
 * @author TSgt Ferguson
 */
@SuppressWarnings({"unchecked"})
@Service("handReceiptDao")
public class HibernateHandReceiptDao extends HibernateGenericDao<HandReceipt> implements HandReceiptDao {

    public HibernateHandReceiptDao() {
        super(HandReceipt.class);
    }

    public List<HandReceipt> getByAdpeAccount(AdpeAccount acct) {

        DetachedCriteria crit = DetachedCriteria.forClass(HandReceipt.class);
        crit.add(Restrictions.eq("adpeAccount", acct));

        return (List<HandReceipt>) getHibernateTemplate().findByCriteria(crit);
    }

    public List<HandReceipt> getByUser(User user) {

        DetachedCriteria crit = DetachedCriteria.forClass(HandReceipt.class);
        crit.add(Restrictions.eq("user", user));

        return (List<HandReceipt>) getHibernateTemplate().findByCriteria(crit);
    }

    public HandReceipt getByName(String name) {

        DetachedCriteria crit = DetachedCriteria.forClass(HandReceipt.class);
        crit.add(Restrictions.ilike("name", name));

        List<HandReceipt> results = getHibernateTemplate().findByCriteria(crit);
        if (results.size()==0) {
            return null;
        }

        return results.get(0);
    }
}
